// pages/book/chapterlist/index.js
const $api = require("../../../api/book").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    albumlist:wx.getStorageSync('albumlist')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 根据专辑id获取选集列表
  getBookInfoByVoiceId(){
    var t = this,
    data = {
      booksVoiceID:t.data.albumlist.books_voice_id,
      member_id:wx.getStorageSync('memberinfo').member_id
    }
    $api.getBookInfoByVoiceId(data).then(res=>{
      console.log(res)
      wx.setStorageSync('albumlist',res.data.chapters )
      t.setData({
        albumlist:res.data.chapters,
        info:res.data.book_info
      })
    })
  },
  // 跳转详情
  gotobookinfo(e){
    var t = this
    wx.navigateTo({
      url:'/pages/book/info/index?id='+e.currentTarget.dataset.id + '&books_type='+t.data.books_type + '&chapter_index=' + e.currentTarget.dataset.index
    })
  },
  gotoalbuminfo(e){
    var t = this
    wx.navigateTo({
      url:'/pages/book/albuminfo/index'
    })
  },
  // 收藏书籍/章节
  insertBookOneCollection(e){
    var t = this,
    index = e.currentTarget.dataset.index,
    data = {
      member_id:wx.getStorageSync('memberinfo').member_id,
      booksVoiceID:t.data.albumlist[index].books_voice_id,
      booksVoiceChapterID:t.data.albumlist[index].books_voice_chapter_id,
      books_type:t.data.books_type,
    }
    $api.insertBookOneCollection(data).then(res=>{
      console.log(res)
      t.getBookInfoByVoiceId()
    })
  },
  // 取消收藏书籍/章节
  cancelBookCollection(e){
    var t = this,
    index = e.currentTarget.dataset.index,
    data = {
      member_id:wx.getStorageSync('memberinfo').member_id,
      booksVoiceID:t.data.albumlist[index].books_voice_id,
      booksVoiceChapterID:t.data.albumlist[index].books_voice_chapter_id,
    }
    $api.cancelBookCollection(data).then(res=>{
      console.log(res)
      t.getBookInfoByVoiceId()
    })
  },
})